<template>
  <div>
    <div style="display: flex ; justify-content: left">
      <el-input v-model="jobLevel.name" placeholder="请输入内容" style="width: 300px "></el-input>
      <el-select v-model="jobLevel.titleLevel" placeholder="请选择">
        <el-option
            v-for="(item,index) in this.titleLevels"
            :key="index"
            :label="item"
            :value="item">
        </el-option>
      </el-select>
      <el-button type="primary" size="mini" @click="doAddJobLevel">主要按钮</el-button>

    </div>
    <div>
      <el-table
          :v-loading="loading"
          :data="joblevels"
          border
          style="width: 100%">
        <el-table-column
            prop="id"
            label="编号"
            width="50">
        </el-table-column>
        <el-table-column
            prop="name"
            label="职称名称"
            width="120">
        </el-table-column>
        <el-table-column
            prop="titleLevel"
            label="职称级别"
            width="120">
        </el-table-column>
        <el-table-column
            prop="createDate"
            label="创建时间"
            width="120">
        </el-table-column>
        <el-table-column
            prop="enabled"
            label="是否启用"
            width="150">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.enabled" active-text="启用" inactive-text="禁用"></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="openEditJobLevelView(scope.row)" type="text" size="small">编辑</el-button>
            <el-button @click="doDeleteJobLevel(scope.row)" type="text" size="small">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="this.currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="this.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="this.total">
      </el-pagination>
    </div>

    <div>
      <el-dialog title="编辑职称信息" :visible.sync="dialogVisible" width="40%">
        <table>
          <tr>
            <td>名称</td>
            <td>
              <el-input v-model="updateJobLevel.name"></el-input>
            </td>
          </tr>
          <tr>
            <td>级别</td>
            <td>
              <el-select v-model="updateJobLevel.titleLevel">
                <el-option
                    v-for="(item,index) in this.titleLevels"
                    :key="index"
                    :label="item"
                    :value="item">
                </el-option>
              </el-select>
            </td>
          </tr>
          <tr>
            <td> 是否启用</td>
            <td>
              <el-switch v-model="this.updateJobLevel.enabled" active-text="启用" inactive-text="禁用"></el-switch>
            </td>
          </tr>
        </table>


        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="doUpdateJobLevel">确 定</el-button>
        </span>
      </el-dialog>
    </div>

  </div>
</template>

<script>
export default {
  name: "JobLevelMana",
  data() {
    return {

      joblevels: [],
      total:0,
      pageSize: 10,
      currentPage: 1,
      loading: false,
      dialogVisible: false,
      jobLevel: {
        name: '',
        titleLevel: '',

      },
      updateJobLevel: {
        name: '',
        titleLevel: '',
        enabled: null,
      },
      titleLevels: ['正高级', '副高级', '中级', '初级', '员级']

    }
  },
  mounted() {
    this.init_joblevel();
  },
  methods: {
    doDeleteJobLevel(rowData){
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
          this.deleteRequest("/system/basic/joblevel/"+rowData.id).then((resp)=>{
            this.$message.success(resp.msg)
          })
        this.init_joblevel()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.init_joblevel();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.init_joblevel();
    },
    doUpdateJobLevel() {
      this.putRequest("/system/basic/joblevel/", this.updateJobLevel).then((resp) => {
        this.dialogVisible= false;
        this.init_joblevel()
      })
    },
    openEditJobLevelView(rowData) {
      this.dialogVisible = true;
      Object.assign(this.updateJobLevel, rowData)
    },
    doAddJobLevel() {
      this.postRequest("/system/basic/joblevel/", this.jobLevel).then((resp) => {
        this.init_joblevel();
        this.jobLevel = {};
        if (resp) {
        }
      })
    },
    init_joblevel() {
      this.loading = true;
      // this.getRequest("/system/basic/joblevel/").then((resp) => {
      //   this.joblevels = resp;
      // })
      this.getRequest("/system/basic/joblevel/page?currentPage="+this.currentPage+"&pageSize="+this.pageSize).then((resp) => {
        if (resp){
          this.joblevels = resp.data;
          this.total = resp.total;
        }
      })
    }

  },

}
</script>

<style scoped>

</style>